package com.account.boot.service.admin.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.account.boot.commons.exception.APIException;
import com.account.boot.commons.result.ResultCodeEnum;
import com.account.boot.commons.result.ResultDO;
import com.account.boot.interfaces.api.UserService;
import com.account.boot.model.vo.UserVo;
import com.account.boot.service.admin.UserMapperService;

@Service
public class UserServiceImpl implements UserService{

	private Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	private UserMapperService userMapperService;
	
	/**
	 * 登录
	 */
	public ResultDO<UserVo> login(String name, String password) {
		ResultDO<UserVo> resultDO = ResultDO.buildResult();
		try {
			UserVo userVo = userMapperService.findByName(name);
			if(userVo == null || !userVo.getPassword().equals(password)) {
				throw new APIException(ResultCodeEnum.API_IS_NOT_USER);
			}
			resultDO.setModule(userVo);
		} catch (APIException e) {
			logger.error("method:login name:" + name + " password:" + password);
			resultDO.setSuccess(false);
			resultDO.setResultCode(e.getResultCode());
			resultDO.setErrorMessage(e.getErrorMessage());
		} catch (Exception e) {
			logger.error("method:login name:" + name + " password:" + password);
			resultDO.setSuccess(false);
			resultDO.setResultCode(ResultCodeEnum.UNKNOW.getResultCode());
			resultDO.setErrorMessage(ResultCodeEnum.UNKNOW.getMessage());
		}
		return resultDO;
	}

	/**
	 * 分页获取用户
	 */
	public ResultDO<List<UserVo>> findByPage(Integer index) {
		ResultDO<List<UserVo>> resultDO = ResultDO.buildResult();
		try {
			resultDO.setModule(userMapperService.findByPage(index));
		} catch (APIException e) {
			logger.error("method:findByPage index:" + index);
			resultDO.setSuccess(false);
			resultDO.setResultCode(e.getResultCode());
			resultDO.setErrorMessage(e.getErrorMessage());
		} catch (Exception e) {
			logger.error("method:findByPage index:" + index);
			resultDO.setSuccess(false);
			resultDO.setResultCode(ResultCodeEnum.UNKNOW.getResultCode());
			resultDO.setErrorMessage(ResultCodeEnum.UNKNOW.getMessage());
		}
		return resultDO;
	}
	
	/**
	 * 通过uid获取用户信息
	 */
	public ResultDO<UserVo> findByUid(String uid) {
		ResultDO<UserVo> resultDO = ResultDO.buildResult();
		try {
			UserVo userVo = userMapperService.findByUid(uid);
			if(userVo == null) {
				throw new APIException(ResultCodeEnum.ADMIN_GET_USERINFO_ERROR);
			}
			resultDO.setModule(userVo);
		} catch (APIException e) {
			logger.error("findByUid uid:" + uid);
			resultDO.setSuccess(false);
			resultDO.setResultCode(e.getResultCode());
			resultDO.setErrorMessage(e.getErrorMessage());
		} catch (Exception e) {
			logger.error("findByUid uid:" + uid);
			resultDO.setSuccess(false);
			resultDO.setResultCode(ResultCodeEnum.UNKNOW.getResultCode());
			resultDO.setErrorMessage(ResultCodeEnum.UNKNOW.getMessage());
		}
		return resultDO;
	}
}
